﻿using System;
using System.Data.Entity.Migrations;
using DAL_Entity;
using DBModel;

namespace CV_Projekt
{
    public partial class FormPublisher : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.MaintainScrollPositionOnPostBack = true;

            var user = Session["User"] as Publisher;

            if (user == null)
            {
                Response.Redirect("Default.aspx");
            }

            if (!IsPostBack)
            {
                BindListBoxAdverts();
                BindDeleted();
            }

            Calendar.SelectedDate = DateTime.Now.Add(TimeSpan.FromDays(30));
        }

        private void BindListBoxAdverts()
        {
            var user = Session["User"] as Publisher;
            if (user != null)
            {
                var workAdvertisement = WorkAdvertisementDB.GetWorkAdvertisementsByPublisher(user.UserId);

                ListBoxAdverts.DataSource = workAdvertisement;
                ListBoxAdverts.DataTextField = "WorkTitle";
                ListBoxAdverts.DataValueField = "WorkAdvertisementId";
                ListBoxAdverts.DataBind();
            }

        }

        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            var user = Session["User"] as Publisher;

            var workAdvertisement = new WorkAdvertisement
            {
                
                ContactDescription = TextBoxContactDescription.Text,
                EmploymentDurationInDays = TextBoxEmploymentDurationInDays.Text,
                LastApplicationDate = Calendar.SelectedDate,
                PublishedDate = DateTime.Now,
                PublisherUserId = user.UserId,
                SalaryType = TextBoxSalaryType.Text,
                WorkDescription = TextAreaWorkDescription.Value,
                WorkLocation = TextBoxWorkLocation.Text,
                WorkTitle = TextBoxWorkTitle.Text,
            };


            

            DB.Context.WorkAdvertisements.AddOrUpdate(workAdvertisement);
            DB.Save();

            BindListBoxAdverts();

            LabelSaved.Visible = true;


        }

        protected void ButtonDelete_Click(object sender, EventArgs e)
        {
            WorkAdvertisementDB.Delete(int.Parse(ListBoxAdverts.SelectedItem.Value));
            BindDeleted();
            BindListBoxAdverts();
            LabelSaved.Visible = true;
        }

        private void BindDeleted()
        {
            var user = Session["User"] as Publisher;
            if (user != null)
            {
                var adverts = WorkAdvertisementDB.GetAllDeletedByUserId(user.UserId);
                ListBoxDeleted.DataSource = adverts;
                ListBoxDeleted.DataTextField = "WorkTitle";
                ListBoxDeleted.DataValueField = "WorkAdvertisementId";
                ListBoxDeleted.DataBind();
            }

        }

        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            DB.Save();
            LabelSaved.Visible = false;
        }

        protected void ListBoxAdverts_SelectedIndexChanged(object sender, EventArgs e)
        {
            var advert = WorkAdvertisementDB.GetWorkAdvertisementById(int.Parse(ListBoxAdverts.SelectedItem.Value));

            TextBoxEmploymentDurationInDays.Text = advert.EmploymentDurationInDays;
            TextBoxSalaryType.Text = advert.SalaryType;
            TextBoxContactDescription.Text = advert.ContactDescription;
            TextBoxWorkLocation.Text = advert.WorkLocation;
            TextBoxWorkTitle.Text = advert.WorkTitle;
            Calendar.SelectedDate = advert.LastApplicationDate;
            TextAreaWorkDescription.Value = advert.WorkDescription;
        }

        protected void ButtonUnDelete_Click(object sender, EventArgs e)
        {
            WorkAdvertisementDB.Restore(int.Parse(ListBoxDeleted.SelectedItem.Value));
            BindDeleted();
            BindListBoxAdverts();
        }

        protected void ButtonUpdate_Click(object sender, EventArgs e)
        {
            var user = Session["User"] as Publisher;

            int workAdvertisementId;

            if (int.TryParse(ListBoxAdverts.SelectedItem.Value, out workAdvertisementId))
            {
                var workAdvertisement = new WorkAdvertisement
                {
                    WorkAdvertisementId = workAdvertisementId,
                    ContactDescription = TextBoxContactDescription.Text,
                    EmploymentDurationInDays = TextBoxEmploymentDurationInDays.Text,
                    LastApplicationDate = Calendar.SelectedDate,
                    PublishedDate = DateTime.Now,
                    PublisherUserId = user.UserId,
                    SalaryType = TextBoxSalaryType.Text,
                    WorkDescription = TextAreaWorkDescription.Value,
                    WorkLocation = TextBoxWorkLocation.Text,
                    WorkTitle = TextBoxWorkTitle.Text,
                };

                var advertToUpdate = WorkAdvertisementDB.GetWorkAdvertisementById(workAdvertisement.WorkAdvertisementId);

                advertToUpdate.CopyNotNullProperties(workAdvertisement);
                DB.Context.WorkAdvertisements.AddOrUpdate(advertToUpdate);
                DB.Save();

                BindListBoxAdverts();

                LabelSaved.Visible = true;
            }





        }

    }
}